package site.xuxingcan;

import site.xuxingcan.util.*;

import javax.swing.*;
import java.io.File;

/**
 * Created by xuxingcan on 16-9-21.
 */
public class MainUI {
    private static JFrame frame;
    private JPanel panel1;
    private JPanel img;
    private JLabel label;
    private JLabel help_text;
    private static final String ROOT_PATH = System.getProperty("user.dir");
    private static final String QR_NAME = "qr.png";

    public static void main(String[] args) {
        System.setProperty("https.protocols", "TLSv1");
        System.setProperty("jsse.enableSNIExtension", "false");
        frame = new JFrame("MainUI");
        frame.setContentPane(new MainUI().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setSize(430, 500);
        frame.setResizable(false);
    }

    public MainUI() {
//        SwingUtilities.invokeLater(new Runnable() {
//            @Override
//            public void run() {
//
//            }
//        });
        help_text.setText("获取二维码......");
        new Thread(new Runnable() {
            @Override
            public void run() {
                GlobalUtil.UUID = WeiXinUtil.getUUID();
                if (GlobalUtil.UUID == null) {
                    help_text.setText("UUID获取失败！");
                } else {
                    String url = "https://login.weixin.qq.com/qrcode/" + GlobalUtil.UUID;
                    if (HttpUtil.downLoadFromUrl(url, ROOT_PATH, QR_NAME)) {
                        label.setIcon(new ImageIcon(new File(ROOT_PATH, QR_NAME).getAbsolutePath()));
                        help_text.setText("扫描二维码登录");
                        frame.setSize(430, 500);
                        //等待扫描登录
                        for (int i = 0; i < 5 * 60 / 2; i++) {
                            if (!WeiXinUtil.waitForLogin().equals("200")) {
                                try {
                                    Thread.sleep(2000);
                                } catch (InterruptedException e) {
                                }
                            } else {
                                break;
                            }
                        }
                        //扫描登录成功之后
                        HttpUtil.CookieAndRes cookieAndRes = HttpUtil.sendGetReturnCookie(GlobalUtil.redirect_uri);
                        GlobalUtil.cookie = cookieAndRes.cookie;
                        GlobalUtil.skey = Matchers.match("<skey>(\\S+)</skey>", cookieAndRes.res);
                        GlobalUtil.wxsid = Matchers.match("<wxsid>(\\S+)</wxsid>", cookieAndRes.res);
                        GlobalUtil.wxuin = Matchers.match("<wxuin>(\\S+)</wxuin>", cookieAndRes.res);
                        GlobalUtil.pass_ticket = Matchers.match("<pass_ticket>(\\S+)</pass_ticket>", cookieAndRes.res);
                        WeiXinUtil.WXinit();
                        help_text.setText("登录成功,当前账号为：" + GlobalUtil.nickName);
                        label.setIcon(null);
                        label.setVisible(false);
                        frame.setSize(300, 100);
                        WeiXinUtil.wxStatusNotify();
                        WeiXinUtil.getContact();
                        boolean exit = false;
                        int mLooperCount = 0;
                        while (!exit) {
                            int[] arr = {0, 0};
                            try {
                                arr = WeiXinUtil.syncCheck();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            LogUtil.info("arr = " + arr[0] + " " + arr[1]);
                            if (arr[0] == 1100 || arr[0] == 1101) {
                                if (mLooperCount < 5) {
                                    LogUtil.info("[*] 你在手机上登出了微信，但是不退出！！！！！");
                                } else {
                                    LogUtil.info("[*] 你在手机上登出了微信，再见");
                                    help_text.setText("已退出登录：" + GlobalUtil.nickName);
                                    exit = true;
                                    break;
                                }
                            }
                            if (arr[0] == 0) {
                                if (arr[1] == 0) {
                                    try {
                                        Thread.sleep(1000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                } else {
                                    String data = null;
                                    try {
                                        data = WeiXinUtil.webwxsync();
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                    if (arr[1] == 2) {
                                        HandleUtil.HandleMsg(data);
                                    } else if (arr[1] == 6) {
                                        HandleUtil.HandleMsg(data);
                                    } else if (arr[1] == 7) {
                                        LogUtil.info("[*] 你在手机上玩微信被我发现了");
                                    } else if (arr[1] == 4) {
                                        HandleUtil.HandleMsg(data);
                                    } else if (arr[1] == 3) {
                                    }
                                }
                            } else {
                                try {
                                    Thread.sleep(1000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                            }
                            if (mLooperCount < 1000) {
                                mLooperCount++;
                            }
                        }
                    } else {
                        help_text.setText("二维码下载失败！");
                    }
                }
            }
        }).start();
    }
}
